﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Mobile.ContainerModel;

namespace Gwn.Library.MvpVm.Extensions
{
    /// <summary>
    /// Add, Edit, Undo, Delete extension
    /// </summary>
    public static class AddEditUndoDeleteExtension
    {
        /// <summary>
        /// Gets the state.
        /// </summary>
        /// <param name="currentStateObj">The current state (button clicked)</param>
        /// <param name="queryStateObj">The query state</param>
        /// <returns>
        /// Tuple not available for phone so we'll make due
        /// </returns>
        public static bool GetState(this object currentStateObj, object queryStateObj)
        {
            var currentState = currentStateObj.ToString();
            var queryState = queryStateObj.ToString();

            var isEditing = currentState.Contains("Add") ||
                            currentState.Contains("Edit");


            // If the state is undo then we want to return
            // the queryState of true for all request except undo
            var isCurrentStateUndo = currentState.Contains("Undo") || currentState.Contains("Save");
            var isQueryStateUndo = queryState.Contains("Undo") || queryState.Contains("Save");

            var isCurrentStateDelete = currentState.Contains("Del")||currentState.Contains("Login");

            // If the delete key is pressed then we'll want to return
            // true for all keys except undo (maintain state)
            if (isCurrentStateDelete)
                return !isQueryStateUndo;

            // If undo was clicked then we want the undo button to 
            // to return false
            if (isCurrentStateUndo && (queryState == currentState))
                return false;

            // if undo and queryState is not undo then return true
            if (isCurrentStateUndo && !isQueryStateUndo)
                return true;

            // if any key other than undo is clicked and the key
            // is not undo then return true
            var isAnyOtherThanUndo = !isCurrentStateUndo && isQueryStateUndo;

            // Return the results
            return isAnyOtherThanUndo;

        }
    }
}
